package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.pojo.UmsMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UmsMenuImpl implements UmsMenuApi {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    /**
     * 添加后台菜单
     *
     * @param umsMenu
     */
    @Override
    public boolean create(UmsMenu umsMenu) {
        //获取菜单名称
        String title = umsMenu.getTitle();
        //获取前端名称
        String name = umsMenu.getName();
        //判断是否数据库已存在,存在不能添加
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getTitle, title);
        int count = umsMenuMapper.selectCount(wrapper);
        LambdaQueryWrapper<UmsMenu> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(UmsMenu::getName, name);
        int count1 = umsMenuMapper.selectCount(wrapper);
        if (count > 0 || count1 > 0) {
            return false;
        }
        if (umsMenu.getParentId() == 0) {
            umsMenu.setLevel(0);
        } else {
            umsMenu.setLevel(1);
        }
        umsMenuMapper.insert(umsMenu);
        return true;
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        umsMenuMapper.deleteById(id);
    }

    /**
     * 分页查询后台菜单
     *
     * @param parentId 父级id,0是一级菜单,1是二级菜单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<UmsMenu> findMenuByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        IPage<UmsMenu> iPage = new Page<>(pageNum, pageSize);
        umsMenuMapper.selectPage(iPage, new LambdaQueryWrapper<UmsMenu>().eq(UmsMenu::getParentId, parentId));
        return iPage;
    }

    /**
     * 修改后台菜单,添加判断,重复不可修改
     *
     * @param id
     * @param umsMenu
     * @return
     */
    @Override
    public void editById(Integer id, UmsMenu umsMenu) {
        umsMenuMapper.updateById(umsMenu);
    }

    /**
     * 修改菜单显示状态
     *
     * @param hidden
     * @param id
     * @return
     */
    @Override
    public void editHidden(Integer id, Integer hidden) {
        UmsMenu umsMenu = umsMenuMapper.selectById(id);
        umsMenu.setHidden(hidden);
        umsMenuMapper.updateById(umsMenu);
        //判断下方是否有子菜单
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId, id);
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(wrapper);
        if (umsMenus != null) {
            for (UmsMenu menu : umsMenus) {
                menu.setHidden(hidden);
                umsMenuMapper.updateById(menu);
            }
        }
    }


    /**
     * 根据ID获取菜单详情
     *
     * @param id
     * @return
     */
    @Override
    public UmsMenu findOneById(Integer id) {
        return umsMenuMapper.selectById(id);
    }


    //根据ids查询对应的菜单集合
    @Override
    public List<UmsMenu> findByIds(List<Long> menuIds) {

        List<UmsMenu> menus = umsMenuMapper.selectBatchIds(menuIds);
        return menus;

    }

    /**
     * 获取一级菜单
     *
     * @return
     */
    @Override
    public List<UmsMenu> findFirstAll() {
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new LambdaQueryWrapper<UmsMenu>().eq(UmsMenu::getParentId, 0));
        return umsMenus;
    }

    /**
     * 获取二级菜单
     *
     * @return
     */
    @Override
    public List<UmsMenu> findSecondAll(Long id) {
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new LambdaQueryWrapper<UmsMenu>().eq(UmsMenu::getParentId, id));
        return umsMenus;
    }

    /**
     * 查询所有菜单的父级id
     *
     * @return
     */
    @Override
    public List<Integer> findParendId() {
        List<Integer> list = new ArrayList<>();
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(new LambdaQueryWrapper<UmsMenu>());
        for (UmsMenu umsMenu : umsMenus) {
            list.add(Convert.toInt(umsMenu.getParentId()));
        }
        return list;
    }

    /**
     * 判断菜单是否重复
     *
     * @param umsMenu
     * @return
     */
    @Override
    public boolean isExists(UmsMenu umsMenu) {
        LambdaUpdateWrapper<UmsMenu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UmsMenu::getTitle, umsMenu.getTitle());
        UmsMenu menu = umsMenuMapper.selectOne(wrapper);
        if (menu != null) {
            return false;
        } else {
            return true;
        }
    }

}
